package com.lmx.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lmx.persistence.service.DataProcessService;
import com.lmx.persistence.service.HuaweiService;
import com.lmx.persistence.service.MessageService;
import com.lmx.persistence.service.TaskService;
import com.lmx.repoDomain.tmodel.TaskPageInfo;
import com.lmx.repoDomain.tmodel.UserTaskPageInfo;

@Controller
@RequestMapping("/backStand")
public class TaskController{

	private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    TaskService taskService;
    
    @Autowired
    MessageService messageService;
    
    @Autowired
    DataProcessService dataProcessService;
    
    @Autowired
    HttpServletRequest request;

	@Autowired
	private HuaweiService huaweiService;
    
    //获取企业任务列表
    @RequestMapping(value = "/getTaskList", method = RequestMethod.GET)
    public String  getTaskList(ModelMap model,TaskPageInfo taskPageInfo) {
    	
    	Map<String ,Object> values = new HashMap<String ,Object>();
    	
    	//验证当前页数
    	if(!String.valueOf(taskPageInfo.getPage()).isEmpty()){
    		int pageNum = (taskPageInfo.getPage()-1)*taskPageInfo.getSize();
    		values.put("pageNum", pageNum);
    		values.put("size", taskPageInfo.getSize());
    		model.put("page", taskPageInfo.getPage());
    	}else{
    		model.put("erroMessage", "参数错误");
    		return "page/erro";
    	}
    	
    	String title = taskPageInfo.getTitle();//任务名称
    	values.put("title", title);
    	model.put("title", title==null?title="":title);
    	
    	String entName = taskPageInfo.getEntName();//企业名称
    	values.put("entName", entName);
    	model.put("entName", entName==null?entName="":entName);
    	
    	String callType = taskPageInfo.getCallType();//任务呼叫类型
    	values.put("callType", callType);
    	model.put("callType", callType==null?callType="":callType);
    	
    	String runstatus = taskPageInfo.getRunstatus();//进行状态
    	values.put("runstatus", runstatus);
    	model.put("runstatus", runstatus==null?runstatus="":runstatus);
    	
    	String status = taskPageInfo.getStatus();//任务状态
    	values.put("status", status);
    	model.put("status", status==null?status="":status);
    	
    	//获取企业任务列表
    	List<Map<String ,Object>> taskList = taskService.getTaskList(values);
    	//获取企业任务列表总条数
    	int count = taskService.getTaskListCount(values);
    	
    	model.put("taskList", taskList);
    	model.put("count", count);
    	
        return "page/task/taskList";
    }    
    
    //获取企业任务详情
   /* @RequestMapping(value = "/getTask", method = RequestMethod.GET)
    public String  getTask(ModelMap model,TaskPageInfo taskPageInfo) {
    	//验证taskID
    	Long taskID = taskPageInfo.getTaskID();
    	if(taskID == null){
    		model.put("erroMessage", "参数错误");
    		return "page/erro";
    	}
    	
    	Map<String ,Object> taskMap = taskService.getTask(taskID);
    	
    	model.put("taskMap", taskMap);
    	model.put("taskID", taskID.toString());
    	String url = taskPageInfo.getUrl();
    	model.put("url", url==null?url="":url);
    	HttpSession session = request.getSession(); 
    	session.setAttribute("url", url==null?url="":url); 
    	session.setMaxInactiveInterval(120*60);//设置失效时长为2小时
    	Integer goBackType = taskPageInfo.getGoBackType();
    	model.put("goBackType", goBackType==null?goBackType=1:goBackType);
        return "page/task/task";
    }*/
    
    //任务审核
    /* @RequestMapping(value = "/updateTaskStatus", method = RequestMethod.GET)
    public String  updateTaskStatus(ModelMap model,TaskPageInfo taskPageInfo) {
    	Task task = new Task();
    	//验证taskID
    	Long taskID = taskPageInfo.getTaskID();
    	if(taskID!=null){
    		task.setId(taskID);
    	}else{
    		model.put("erroMessage", "参数错误");
    		return "page/erro";
    	}
    	
    	//验证status
    	String status = taskPageInfo.getStatus();
    	
    	Dicttype dicttype = new Dicttype();
    	dicttype.setId(Long.parseLong(status));
    	logger.debug("status--------------------------------------"+status);
    	if(!status.isEmpty()){
    		task.setStatus(dicttype);
    	}else{
    		model.put("erroMessage", "参数错误");
    		return "page/erro";
    	}
    	String verifyFailReason = taskPageInfo.getVerifyFailReason();
    	task.setVerifyFailReason(verifyFailReason);

		if(dicttype.getId() == ActionContextHolder.getDicctypeHolder().get("taskIsPassed").getId()){
			logger.debug("任务审核通过");
			huaweiService.addTaskIms(task.getId());//添加总机
		}
		taskService.updateTaskStatus(task);
    	//发送任务审核提醒消息
    	messageService.companyTaskRemind(task);
    	
    	Map<String ,Object> taskMap = taskService.getTask(task.getId());
    	HttpSession session = request.getSession(); 
    	model.put("url", session.getAttribute("url"));
    	Integer goBackType = taskPageInfo.getGoBackType();
    	model.put("goBackType", goBackType==null?goBackType=1:goBackType);
    	
    	model.put("taskMap", taskMap);
    	model.put("taskID", taskID.toString());
        return "page/task/task";
    }*/
    
    //获取客服任务列表
    @RequestMapping(value = "/getUserTaskList", method = RequestMethod.GET)
    public String  getUserTaskList(ModelMap model,UserTaskPageInfo userTaskPageInfo) {
    	
    	Map<String ,Object> values = new HashMap<String ,Object>();
    	
    	//验证当前页数
    	if(!String.valueOf(userTaskPageInfo.getPage()).isEmpty()){
    		int pageNum = (userTaskPageInfo.getPage()-1)*userTaskPageInfo.getSize();
    		values.put("pageNum", pageNum);
    		values.put("size", userTaskPageInfo.getSize());
    		model.put("page", userTaskPageInfo.getPage());
    	}else{
    		model.put("erroMessage", "参数错误");
    		return "page/erro";
    	}
    	
    	String loginName = userTaskPageInfo.getLoginName();//用户名
    	values.put("loginName", loginName);
    	model.put("loginName", loginName==null?loginName="":loginName);
    	
    	String name = userTaskPageInfo.getName();//客服名称
    	values.put("name", name);
    	model.put("name", name==null?name="":name);
    	
    	String taskName = userTaskPageInfo.getTaskName();//任务名称
    	values.put("taskName", taskName);
    	model.put("taskName", taskName==null?taskName="":taskName);
    	
    	String bindPhone = userTaskPageInfo.getBindPhone();//绑定手机号
    	values.put("bindPhone", bindPhone);
    	model.put("bindPhone", bindPhone==null?bindPhone="":bindPhone);
    	
    	String status = userTaskPageInfo.getStatus();//客服任务状态
    	values.put("status", status);
    	model.put("status", status==null?status="":status);
    	
    	String phoneNumber = userTaskPageInfo.getPhoneNumber();// 坐席号(操作员)
    	values.put("phoneNumber", phoneNumber);
    	model.put("phoneNumber", phoneNumber==null?phoneNumber="":phoneNumber);
    	
	    String agentNumber = userTaskPageInfo.getAgentNumber();// 固话号（选线组号码）
	    values.put("agentNumber", agentNumber);
    	model.put("agentNumber", agentNumber==null?agentNumber="":agentNumber);
    	
    	//获取客服任务列表
    	List<Map<String ,Object>> userTaskList = taskService.getUserTaskList(values);
    	//获取客服任务列表总条数
    	int count = taskService.getUserTaskListCount(values);
    	
    	model.put("userTaskList", userTaskList);
    	model.put("count", count);
    	
        return "page/task/userTaskList";
    }    
    
    //获取客服任务详情
    @RequestMapping(value = "/getUserTask", method = RequestMethod.GET)
    public String  getUserTask(ModelMap model,UserTaskPageInfo userTaskPageInfo) {
    	//验证userTaskID
    	Long userTaskID = userTaskPageInfo.getUserTaskID();
    	if(userTaskID == null){
    		model.put("erroMessage", "参数错误");
    		return "page/erro";
    	}
    	
    	Map<String ,Object> userTaskMap = taskService.getUserTask(userTaskID);
    	
    	model.put("userTaskMap", userTaskMap);
    	model.put("userTaskID", userTaskID.toString());
    	String url = userTaskPageInfo.getUrl();
    	model.put("url", url==null?url="":url);
    	HttpSession session = request.getSession(); 
    	session.setAttribute("url", url==null?url="":url); 
    	session.setMaxInactiveInterval(120*60);//设置失效时长为2小时
        return "page/task/userTask";
    }
    
    //获取客服操作信息     
    @RequestMapping(value = "/getHandle", method = RequestMethod.GET)
    public String  getHandle(ModelMap model,UserTaskPageInfo userTaskPageInfo) {
    	//验证taskID
    	Long userTaskID = userTaskPageInfo.getUserTaskID();
    	if(userTaskID == null){
    		model.put("erroMessage", "参数错误");
    		return "page/erro";
    	}
    	//获取客服操作信息
    	Map<String ,Object> userTaskMap = taskService.getUserTaskPhoneNumAndAgentNuk(userTaskID);
    	
    	model.put("userTaskMap", userTaskMap);
    	model.put("userTaskID", userTaskID.toString());
    	String url = userTaskPageInfo.getUrl();
    	model.put("url", url==null?url="":url);
    	HttpSession session = request.getSession(); 
    	session.setAttribute("url", url==null?url="":url); 
    	session.setMaxInactiveInterval(120*60);//设置失效时长为2小时
        return "page/task/updateUserTask";
    }
    
    //添加客服操作详情页面
    @RequestMapping(value = "/upUserTaskToPage", method = RequestMethod.GET)
    public String  upUserTaskToPage(ModelMap model,UserTaskPageInfo userTaskPageInfo) {
    	//验证taskID
    	Long userTaskID = userTaskPageInfo.getUserTaskID();
    	if(userTaskID == null){
    		model.put("erroMessage", "参数错误");
    		return "page/erro";
    	}
    	//获取客服操作信息
    	Map<String ,Object> userTaskMap = taskService.getUserTaskPhoneNumAndAgentNuk(userTaskID);
    	
    	model.put("userTaskMap", userTaskMap);
    	model.put("userTaskID", userTaskID.toString());
    	String url = userTaskPageInfo.getUrl();
    	model.put("url", url==null?url="":url);
    	HttpSession session = request.getSession(); 
    	session.setAttribute("url", url==null?url="":url); 
    	session.setMaxInactiveInterval(120*60);//设置失效时长为2小时
        return "page/task/updateUserTask";
    }
    
    //客服开通操作
/*    @RequestMapping(value = "/updateUserTask", method = RequestMethod.GET)
    @ResponseBody
    public String  updateUserTask(String userTaskID,String phoneNumber,String phonePassword,String agentNumber,String agentPassword) {
    	String json = "";
    	Map<String ,Object> msgMap = new HashMap<String ,Object>();
    	UserTask userTask = new UserTask();
    	//验证userTaskID
    	if(userTaskID!=null){
    		userTask.setId(Long.parseLong(userTaskID));
    	}
    	
    	//验证phoneNumber
    	if(phoneNumber != null && !phoneNumber.isEmpty()){
    		userTask.setPhoneNumber(phoneNumber);
    	}
    	
    	//验证phonePassword
    	if(phonePassword != null && !phonePassword.isEmpty()){
    		userTask.setPhonePassword(phonePassword);
    	}
    	//验证agentNumber
    	if(agentNumber != null && !agentNumber.isEmpty()){
    		userTask.setAgentNumber(agentNumber);
    	}
    	//验证agentPassword
    	if(agentPassword != null && !agentPassword.isEmpty()){
    		userTask.setAgentPassword(agentPassword);
    	}
    	//添加操作信息
    	userTask = taskService.updateUserTask(userTask);
    	//更新该任务客服数量
    	dataProcessService.updateCsNum(userTask);
    	//给客服发送消息提醒
    	String type = "taskApplySucc";//已通过
    	messageService.taskRemind(userTask,type);
    	
    	HttpSession session = request.getSession(); 
    	
    	msgMap.put("flag", "1");//添加成功
    	msgMap.put("url", session.getAttribute("url"));//添加成功
    	
    	json = CommonUtil.mapToJsonString(msgMap);
        return json;
    }*/
    
}
